package com.ytf.core.web.application;

import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.ytf.core.common.CacheOperatorApi;
import com.ytf.core.web.pojo.converter.PermissionConverter;
import com.ytf.core.web.pojo.dto.PermissionDto;
import com.ytf.core.web.pojo.model.Permission;
import com.ytf.core.web.pojo.vo.PermissionVo;
import com.ytf.core.web.service.IPermissionService;
import com.ytf.core.web.service.IRolePermissionService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author YTF
 * @className PermissionAppService
 * @date 2025/5/20 21:53
 * @description 权限应用层
 */
@Service
public class PermissionAppService {

    @Autowired
    private IPermissionService iPermissionService;
    @Autowired
    private IRolePermissionService iRolePermissionService;

    @Resource(name = "permissionCache")
    private CacheOperatorApi<PermissionVo> permissionCache;

    public Boolean insertPermission(PermissionDto permissionDto){
        Permission permission = PermissionConverter.INSTANCE.permissionDtoToPermission(permissionDto);
        boolean save = iPermissionService.save(permission);
        // 添加缓存
        if(save) {
            permissionCachePut(permission);
        }
        return save;
    }

    @Transactional(rollbackFor = Exception.class)
    public void deletePermissionById(Long id) {
        boolean removeById = iPermissionService.removeById(id);
        Assert.isTrue(removeById, "删除权限失败");
        Boolean existsByPermissionId = iRolePermissionService.existsByPermissionId(id);
        if(!existsByPermissionId){
            return;
        }
        // 删除缓存
        permissionCache.remove(String.valueOf(id));

        Boolean aBoolean = iRolePermissionService.removeByPermissionId(id);
        Assert.isTrue(aBoolean, "删除用户权限关联失败");
    }

    public PermissionVo getPermissionById(Long id) {
        PermissionVo permissionVo = permissionCache.get(String.valueOf(id));
        if (permissionVo != null) {
            return permissionVo;
        }
        Permission byId = iPermissionService.getById(id);
        // 添加缓存
        return permissionCachePut(byId);
    }

    public Boolean updatePermissionById(Long id, PermissionDto permissionDto) {
        Boolean existsById = iPermissionService.existsById(id);
        Assert.isTrue(existsById,"权限id查询不存在");
        Boolean existsByPermKeyAndIdNot = iPermissionService.existsByPermKeyAndIdNot(permissionDto.getPermKey(),id);
        Assert.isFalse(existsByPermKeyAndIdNot,"权限标识已存在");
        Permission permission = PermissionConverter.INSTANCE.permissionDtoToPermission(permissionDto);
        permission.setId(id);
        boolean updateById = iPermissionService.updateById(permission);
        // 更新缓存
        permissionCachePut(permission);
        return updateById;
    }

    /**
     * 初始化全部权限
     */
    public void init() {
        System.out.println("权限初始化");
        List<Permission> list = iPermissionService.list();
        list.forEach(this::permissionCachePut);
        System.out.println(permissionCache.getAllKeyValues());
    }

    private PermissionVo permissionCachePut(Permission permission) {
        PermissionVo permissionVo = PermissionConverter.INSTANCE.permissionToPermissionVo(permission);
        permissionCache.put(String.valueOf(permission.getId()), permissionVo);
        return permissionVo;
    }
}
